﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace CharacterFactory
{
    class ImageProcessing
    {
        public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }

        public List<Bitmap> mergeSprite(List<Bitmap> listBitmap, Color transparency)
        {

            List<Bitmap> result = new List<Bitmap>();
            int numberOfFileToCreate = 1;
            int numberOfRangePerFile = 1;
            int numberOfTilePerRange = 0;
            int biggerWidth = 0;
            int biggerHeight = 0;

            foreach (Bitmap b in listBitmap)
            {
                if (biggerWidth < b.Width)
                {
                    biggerWidth = b.Width;
                }
                if (biggerHeight < b.Height)
                {
                    biggerHeight = b.Height;
                }
            }

            for (int numberOfTile = 1; numberOfTile <= listBitmap.Count; numberOfTile++)
            {
                if (biggerWidth * numberOfTile <= 2048)
                {
                    numberOfTilePerRange++;
                }
                else
                {
                    numberOfRangePerFile++;
                    for (int n = biggerHeight * numberOfRangePerFile; n > 2048; n = n - 2048)
                    {
                        numberOfFileToCreate++;
                    }
                }
            }

            int rangeWidth = numberOfTilePerRange * biggerWidth;

            Bitmap a = new Bitmap(rangeWidth, biggerHeight * numberOfRangePerFile);
            result.Add(a);

            int tile = 0;
            int range = 1;
            int offsetX = 0;
            int offsetY = 0;


            foreach (Bitmap b in listBitmap)
            {
                for (int x = 0; x < biggerWidth; x++)
                {
                    for (int y = 0; y < biggerHeight; y++)
                    {
                        if (x < b.Width)
                        {
                            if (y < b.Height)
                            {
                                result[result.Count - 1].SetPixel(offsetX + x, offsetY + y, b.GetPixel(x, y));
                            }
                            else
                            {
                                result[result.Count - 1].SetPixel(offsetY + x, offsetY + y, transparency);
                            }
                        }
                        else
                        {
                            result[result.Count - 1].SetPixel(offsetY + x, offsetY + y, transparency);
                        }
                    }
                }

                offsetX = offsetX + biggerWidth;
                tile++;
                if (tile == numberOfTilePerRange)
                {
                    if (range <= numberOfRangePerFile)
                    {
                        offsetX = 0;
                        offsetY = biggerHeight * range;
                        range++;
                    }
                    else
                    {
                        result.Add(a);
                    }
                }
            }

            return result;
        }

        public Size getSpriteSize(List<Bitmap> listBitmap)
        {
            Size s = new Size();
            int numberOfFileToCreate = 1;
            int numberOfRangePerFile = 1;
            int numberOfTilePerRange = 0;
            int biggerWidth = 0;
            int biggerHeight = 0;

            foreach (Bitmap b in listBitmap)
            {
                if (biggerWidth < b.Width)
                {
                    biggerWidth = b.Width;
                }
                if (biggerHeight < b.Height)
                {
                    biggerHeight = b.Height;
                }
            }

            for (int numberOfTile = 1; numberOfTile <= listBitmap.Count; numberOfTile++)
            {
                if (biggerWidth * numberOfTile <= 2048)
                {
                    numberOfTilePerRange++;
                }
                else
                {
                    numberOfRangePerFile++;
                    for (int n = biggerHeight * numberOfRangePerFile; n > 2048; n = n - 2048)
                    {
                        numberOfFileToCreate++;
                    }
                }
            }

            int rangeWidth = numberOfTilePerRange * biggerWidth;
            s.Width = biggerWidth;
            s.Height = biggerHeight;

            return s;
        }

        public int createANIFile(List<Tile> listTile, Color transparency,string name)
        {
            int result = 0;
            List<Bitmap> listBmp = new List<Bitmap>();

            foreach (Tile tile in listTile)
            {
                try
                {
                    listBmp.Add(tile.Sprite);
                }
                catch (Exception xcp)
                {
                    MessageBox.Show(xcp.ToString());
                }
            }
            mergeSprite(listBmp, transparency);

            if (listBmp.Count == 0)
            {
                result = -1;
            }
            else
            {
                int index = 0;
                foreach (Bitmap bmp in listBmp)
                {
                    string target = Directory.GetCurrentDirectory() + "\\" + name + ".ani" + index.ToString();
                    bmp.Save(target);
                    index++;
                }
                
            }

            return result;
        }
    }
}
